<?php

namespace app\api\command;
use fast\Random;
use think\console\Command;
use think\console\Input;
use think\console\Output;

class Order extends Command
{
    protected function configure()
    {
        $this->setName('order')->setHelp('订单查询')->setDescription('订单查询');

    }

    protected function execute(Input $input, Output $output)
    {
        $order =  db('order')->where('platform_type',1)->whereIn('order_status','-1,1,2,3,4')->order('id desc')->select();
        if($order){
            $url = "http://agent.haoka.bx10086.cn/v1/haoka_api/orders";//办理套餐
            $r   = '';
            foreach ($order as $k=>$v){
                sleep(2);
//                dump($v['id']);
                $data['appid']     = config('site.ooen_appid');
                $data['timestamp'] = time();
                $data['nonce_str'] = Random::alnum(32);
                $data['order_id']  = $v['order_id'];
                $data['phone']     = $v['shipping_phone'];

                $string            = ascll($data);
                $stringSignTemp    = $string.'&appsecret='.config('site.open_appsecret');
                $data['sign']      = mb_strtoupper(md5($stringSignTemp));

                $res = httpPost($url,$data);
                $arr = json_decode($res,true);
                unset($data);
                if(!empty($arr['rows'][0])){
                    $rows = $arr['rows'][0];
                    $up['order_status']  = $rows['order_status'];
                    if(strpos($rows['remark'],'已激活') !== false){
                        $up['order_status'] = 1;
                    }
                    if(strpos($rows['remark'],'待归档') !== false){
                        $up['order_status'] = 1;
                    }
                    if(strpos($rows['remark'],'充值') !== false){
                        $up['order_status']   = 4;
                        $up['baoxiao_status'] = 1;
                        $up['audit_status']   = 2;
                        $up['jiesuan_time']   = time();
                    }
                    $up['order_info_json'] = json_encode($arr['rows'],true);
                    $up['order_status_text'] = $rows['order_status_text'];
                    $up['rebate']            = $rows['rebate'];
                    $up['channel']           = $rows['channel'];
                    $up['remark']            = $rows['remark'];
                    $up['package_name']      = $rows['package_name']??'';
                    if($rows['phone']){
                        $up['phone']         = $rows['phone'];
                    }
                    if(!empty($rows['shipping_no'])){
                        $up['shipping_no']   = $rows['shipping_no'];
                    }
                    if(!empty($rows['shipping_company_name'])){
                        $up['shipping_company_name'] = $rows['shipping_company_name'];
                    }
                    $up['is_recharged'] = $rows['is_recharged'];
                    $r = db('order')->where(['id'=>$v['id']])->update($up);
                }

            }

        }
        if($r){
            echo "操作成功";
        }else{
            echo "更新失败";
        }
    }

}
